<template>
    <layout>
      <el-form :model="form" :rules="rules" ref="form" label-width="100px" class="form">
        <el-form-item label="用户名" prop="account">
          <el-input disabled v-model="userInfo.account"></el-input>
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码"  prop="password">
          <el-input disabled type="password" v-model="userInfo.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('form')">确定</el-button>
          <!-- <el-button @click="resetForm('form')">重置</el-button> -->
        </el-form-item>
      </el-form>
    </layout>
</template>

<script>
import layout from '@/layout/index.vue'	
export default {
	components: {
		layout,
		},
    data () {
        return {
          form:{
            username:'',
          },
          rules: {
            username: [
              { required: true, message: '请输入姓名', trigger: 'blur' },
            ],
          }
        };
    },
    onLoad(options){
      this.userInfo = uni.getStorageSync('userInfo');
      this.form.username = this.userInfo.username
      this.getUserInfo()
    },
    methods: {
      getUserInfo(){
        const data={
          userId:this.userInfo._id
        }
        this.$Ajax('getUserInfo',data,res=>{
          console.log('getUserInfo',res.data)
          this.form.username = res.data.username
          uni.setStorageSync('userInfo', res.data);
        },err=>{
          console.log('err',err)
        })
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            const data={
              userId:this.userInfo._id,
							username:this.form.username,
						}
						console.log('data',data)
						uni.showLoading({
							title: '提交中',
							mask: true
						});
						this.$Ajax('addUser', data, res => {
							uni.hideLoading();
							if (res.code == 200) {
								this.$tools.toast('操作成功')
                this.getUserInfo()
							} else {
								this.$tools.toast(res.message)
							}
						},err=>{
							uni.hideLoading();
							console.log('err',err)
						})
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    },
};
</script>

<style scoped>
.form{
  width: 80%;
  margin: 20px auto;
}
</style>
